$(function(){
			jQuery.validator.addMethod("isMobile", function(value, element) {
				    var length = value.length;
				    var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
				    return this.optional(element) || (length == 11 && mobile.test(value));
				}, "请正确填写您的手机号码");
				//用户名改变时触发
				var name;
				var pass;
				$("#username").change(function(){
					name = $(this).val();
					
					if($.cookie(name) == null){
						$.validator.setDefaults({
							submitHandler: function() {
						      alert("提交成功!");
						      pass = $('#pwd').val();
						      var arr = new Array();
						      arr.push(name);
						      arr.push(pass);
						      $.cookie('name',arr,{path: "/", expires: 7 })
								console.log('加入');
								window.location.href="login.html";
						    }
						})
						
						// 在键盘按下并释放及提交后验证提交表单
						$().ready(function(){
					// 提交后验证提交表单
							$("#form2").validate({
								rules:{
									username:{
										required: true,
										minlength:11,
										isMobile:true
									},
									pwd:{
										required:true,
										minlength:5
									},
									confpwd:{
										required:true,
										minlength: 5,
										 equalTo: "#pwd"
									},
									cheched_1:{
										required:true,
										minlength:4,
										 equalTo: "#check_num"
									},
									//为了区分字段对齐，往后移动一格
									messages:{
										username:{
											required:"请输入用户名",
											minlength: "手机号有11位",
									        isMobile:"请填写正确的手机号"
										},
										pwd:{
											required: "请输入密码",
									        minlength: "密码长度不能小于 5 个"
										},
										confpwd:{
											required: "请输入确认密码",
										    minlength: "确认密码不能小于5个字符",
										    equalTo: "两次密码输入不一致"
										},
										cheched_1:{
									    	 required: "请输入验证码",
									    	 minlength:"请输入四位的数",
									    	 equalTo: "验证码有误"
									 	},
									}//messages结束
								}
							})
						});
					}else{
						console.log('用户名已存在');
						alert("该用户已注册，请确认信息是否出错")
//						var str1 = $.cookie(name); 　
						console.log($.cookie());
						console.log($.cookie());
					}
				})

				/*-----------获取四位数并给到text中-------------*/
				var str ="";
				var show = function(){
					for(var i=0;i<4;i++){   //获取4位验证码	
						var num = parseInt(Math.random()*62);
						if(num>=0&&num<=9){  //获取0-9的数
						str+=num;			
						}else if(num>=10&&num<36){   // 大写字母
							str+= String.fromCharCode(num+55);				
						}else if(num>=36&&num<=62){  //小写字母
							str+= String.fromCharCode(num+61)									
						}		
					}
					console.log(num)
					$("#check_num").val(str);
				}
				show();
				$("#check_num").click(function(){
					str ="";
					show();
				})
		})